<?php

class Exam_AdminCategoryController extends Core_Controller_Action_Admin {
	/**
	 * category api
	 * @var Auction_Api_Category $categoryApi
	 */
	protected $categoryApi = null;
	
	/**
	 * category tree
	 * @var array $tree
	 */
	protected $tree = null;
	
	public function init() {
		// append stylesheet and script
		$this->view->headLink()          
			->appendStylesheet($this->view->baseUrl('application/modules/Exam/externals/treeBox/css/treeBox/lightBlue/treeBox.css'));
		$this->view->headScript()
			->appendFile($this->view->baseUrl('application/modules/Exam/externals/treeBox/js/treeBox/tw-sack.js'));
		$this->view->headScript()
			->appendFile($this->view->baseUrl('application/modules/Exam/externals/treeBox/js/treeBox/treeBox.js'));
		
		// category api
		$this->categoryApi = Engine_Api::_()
			->getApi('category', 'exam');
			
		// get all categories
		$this->tree = $this->categoryApi->getExamCategoryTree();	
		
	}
	
	public function indexAction() {
		// navigation
		$this->view->navigation = $navigation = Engine_Api::_()
			->getApi('menus', 'core')
			->getNavigation('exam_admin_main', array (), 'exam_admin_main_categories');
	//	$xmlTxt = $this->categoryApi->getAuctionTreeXML($this->tree, true);
		//$this->view->treeXML = $xmlTxt;
		
	}
	
	public function createAction() {
		$this->view->isSuccess = false;
		$form = $this->view->form = new Exam_Form_Admin_Category_Create();
		
		// add options to select element
		$form->parent_id->addMultiOption(0, 'Root');
		
		// get ids
		$cateIds = $this->categoryApi->getExamTreeChildrenCategoryId($this->tree);
		
		foreach ($cateIds as $cateId) {
			$form->parent_id->addMultiOption($cateId, "");
	    }
		
		// add
		if ($this->getRequest()->isPost()) {						
			if ($form->isValid($this->getRequest()->getPost())) {
				$values = $form->getValues();
				// save
				$table = Engine_Api::_()->getDbtable('categories', 'exam');
				$category = $table->createRow();
				$category->category_name = $values['title'];
                if(!empty($values['parent_id'])){
                    $category->parent_id = $values['parent_id'];
                }
                else{
                    $category->parent_id = 0;
                }
				
				$newCategoryId = $category->save();
				$this->view->newCategoryId = $newCategoryId;
				// return
				/*
				return $this->_forward('success', 'utility', 'core', array(
						'messages' => array (
								Zend_Registry::get('Zend_Translate')->_('This category was created.')
						), 
						'layout' => 'default-simple', 
						'parentRefresh' => true
				));*/
				$this->view->isSuccess = true;
			}
			else {				
				$this->view->parentId = $this->_getParam('parent_id', 0);
			}
		}
		else {
			// get input
			$parentId = $this->_getParam('treeBox', 0);
			$this->view->parentId = $parentId;
		}
		
	}
	
	public function editAction() {
		$this->view->isSuccess = false;
		$form = $this->view->form = new Exam_Form_Admin_Category_Edit();
						
		if (!$this->getRequest()->isPost()) {
			// get input
			$id = $this->_getParam('treeBox', 0);
			
			// assign and check
			if (!$form->category_id->isValid($id)) {			 
				return false;
			}			
		}
		else {
			$id = $this->_getParam('category_id');
		}
		
		// set current category id
		$this->view->category_id = $id;
		
		// get info	
		$category = Engine_Api::_()->getItem('exam_category', $id);		
		
		// set title
		$form->setTitle('Edit Category: ' . $category->category_name);
		
		// add options to select element
		$form->parent_id->addMultiOption(0, 'Root');
		
		// get ids
		$cateIds = $this->categoryApi->getExamTreeChildrenCategoryId($this->tree);
		
		foreach ($cateIds as $cateId) {
			// exclude current id
			if ($cateId != $id) {
				$form->parent_id->addMultiOption($cateId, "");
			}
	    }	    
	    
		// edit
		if ($this->getRequest()->isPost()) {
			if ($form->isValid($this->getRequest()->getPost())) {
				$values = $form->getValues();
				
				// save				
				$category->category_name = $values['title'];
				$category->parent_id = $values['parent_id'];
				$category->save();
				
				$this->view->isSuccess = true;
			}
			else {				
				$this->view->parentId = $this->_getParam('parent_id', 0);
			}
		}
		else {
			// set parent
			$this->view->parentId = $category->parent_id;
			
			// set titlle value
	    	$form->title->setValue($category->category_name);
		}
	}
	
	public function deleteAction() {
		$this->view->isSuccess = false;
		$form = $this->view->form = new Exam_Form_Admin_Category_Delete();
		
		if (!$this->getRequest()->isPost()) {
			// get input
			$id = $this->_getParam('treeBox', 0);
			
			// assign and check
			if (!$form->category_id->isValid($id)) {			 
				return false;
			}
		}
		else {
			$id = $this->_getParam('category_id');
		}		
		
		// get info	
		$category = Engine_Api::_()->getItem('exam_category', $id);	
			
		// set parent category id
		$this->view->parentId = $category->parent_id;
		
		// set title
		$form->setTitle('Delete Category: ' . $category->category_name);
		
		// delete
		if ($this->getRequest()->isPost()) {
			if ($form->isValid($this->getRequest()->getPost())) {
				$db = Engine_Api::_()->getDbtable('categories', 'exam')->getAdapter();
				$db->beginTransaction();
				try {
					/*
					 * Get category and it's children
					 */
					// get current category
					$category = $this->categoryApi
						->getExamTreeCategoryById($category->category_id, $this->tree);
					
					// get ids of current category and it's chilldren
					$categoryIds = $this->categoryApi
						->getExamTreeCategoriesId($category);
					
					/*
					 * Delete
					 */
					foreach ($categoryIds as $categoryId) {
						$category = Engine_Api::_()->getItem('exam_category', $categoryId);
						if (!empty($category)) {
							$category->delete();
						}
					}
					
					// Commit
					$db->commit();
					
					$this->view->isSuccess = true;
				}
				catch (Exception $e) {
					$db->rollBack();
					throw $e;
				}
			}
		}
		
	}
}